﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBunny : MonoBehaviour {
    public static EnemyBunny con;

    public void Awake()
    {
        con = this;
    }
    public float speed;
    public LayerMask layer;
    public LayerMask layer1;

    public Transform Lpoint;
    public Transform Rpoint;
    public Transform Cpoint;

    private bool Collid;
    private bool Collid1;

    private Rigidbody2D rg;
    private CapsuleCollider2D ca;
    private Animator an;
    
    // Use this for initialization
    void Start()
    {
        rg = GetComponent<Rigidbody2D>();
        ca = GetComponent<CapsuleCollider2D>();
        an = GetComponent<Animator>();
    }
    
    private void FixedUpdate()
    {
        Collid = Physics2D.OverlapCircle(Cpoint.position,30,layer);
        Collid1= Physics2D.Linecast(Lpoint.position, Rpoint.position, layer1);

        if (Collid)
        {
            an.SetBool("Run", true);
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
            if (Collid1)
            {
                transform.localScale = new Vector3(-transform.localScale.x, 10, 1);
                speed=speed * -1;
            }
        }
        else
        {
            an.SetBool("Run", false);
        }
        
    }
    
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            speed = 0;
            an.SetTrigger("Die");
            ca.enabled = false;
            rg.simulated = false;
            Destroy(gameObject, 1.2f);
            PlayerBloodController.con.SetBlood();
            PlayerController.con.PlayerHurt();
        }
        if (other.gameObject.tag=="sword")
        {
            speed = 0;
            an.SetTrigger("Die");
            ca.enabled = false;
            rg.simulated = false;
            Destroy(gameObject, 1.2f);
            CoinController.con.CreatCoin(Rpoint, 5);
        }
    }
}
